import json

from flask import Blueprint, request

from apps.sys.model.role import Role
from apps.tool import R, objs2dicts

role = Blueprint('sys/role', __name__)


@role.route('get', methods=['GET'])
def role_get():
    """
    获取单个角色信息
    :return:
    """
    roleId = request.args.get("id", 0)
    return R.data(Role.to_dict(Role.query.filter_by(id=roleId).first()))


@role.route('list', methods=['GET'])
def role_list():
    """
    查询角色列表
    :return:
    """
    return R.data(objs2dicts(Role.query.all()))


@role.route('save', methods=['POST'])
def role_save():
    """
    新增角色
    :return:
    """
    data = json.loads(request.get_data(as_text=True))
    u = Role()
    Role.dict2obj(u, data)
    print(u.to_dict())
    return R.success()


@role.route('update', methods=['POST'])
def role_update():
    """
    修改角色
    :return:
    """
    data = json.loads(request.get_data(as_text=True))
    u = Role()
    Role.dict2obj(u, data)
    print(u.to_dict())
    return R.success()


@role.route('remove', methods=['POST'])
def role_remove():
    """
    删除角色
    :return:
    """
    print(request.get_data(as_text=True))
    return R.success()
